package com.gzsxy.esjy.service.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzsxy.esjy.bus.event.InnerProtocolMessageEvent;
import com.gzsxy.esjy.bus.message.InnerProtocol;
import com.gzsxy.esjy.common.base.exception.impl.CustomAssert;
import com.gzsxy.esjy.common.base.result.ResultCodeEnum;
import com.gzsxy.esjy.service.auth.dao.UserMapper;
import com.gzsxy.esjy.service.auth.entity.User;
import com.gzsxy.esjy.service.auth.entity.vo.UserQueryVo;
import com.gzsxy.esjy.service.auth.service.UserService;
import com.gzsxy.esjy.service.base.DictionaryConstants;
import com.gzsxy.esjy.service.base.dto.request.UserRequest;
import com.gzsxy.esjy.service.base.dto.response.OrderRsp;
import com.gzsxy.esjy.service.base.dto.response.UserRsp;
import com.gzsxy.esjy.service.base.util.Page;
import com.gzsxy.esjy.service.base.util.ValidateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.sql.Wrapper;
import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: TODO
 * @date 2022/2/8 9:31
 */
@Service
public class UserServiceImpl  extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * @description: 注册用户
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void registerUser(UserRequest userRequest) {
        if (DictionaryConstants.NUMBER_0.equals(userRequest.getUserType())){
            //ToDo 唯一用户名校验（学号：学校）
            ValidateUtil.assertUnique(userMapper::existByUserName,userRequest.getUserName(),userRequest.getSchool(),ResultCodeEnum.USER_EXIST_ERROR.getMessage());
        }
        //唯一手机号注册
        ValidateUtil.assertUnique(userMapper::existByPhone,userRequest.getPhone(),ResultCodeEnum.REGISTER_MOBLE_ERROR.getMessage());
        User user = new User();
        BeanUtils.copyProperties(userRequest,user);
        if (StringUtils.isEmpty(userRequest.getFreeze())){
            //默认未禁用
            user.setFreeze(DictionaryConstants.NUMBER_0);
        }
        user.setAmount(DictionaryConstants.NUMBER_0);
        baseMapper.insert(user);
    }

    /**
     * @description: 根据id查询用户信息
     * @param
     * @return
     */
    @Override
    public UserRsp getUserById(String id) {
        User user = baseMapper.selectById(id);
        CustomAssert.notTrue(!ObjectUtils.isEmpty(user),ResultCodeEnum.USER_NOT_EXIST_ERROR.getMessage());
        UserRsp userRsp = new UserRsp();
        BeanUtils.copyProperties(user,userRsp);
        return userRsp;
    }

    /**
     * @description: 修改用户
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserRsp
    editUser(UserRequest userRequest) {
        User user = baseMapper.selectById(userRequest.getId());
        CustomAssert.notTrue(!ObjectUtils.isEmpty(user),ResultCodeEnum.USER_NOT_EXIST_ERROR.getMessage());
        User user1 = new User();
        BeanUtils.copyProperties(userRequest,user1);
        baseMapper.updateById(user1);
        //获取更新后的用户信息
        User user2 = baseMapper.selectById(userRequest.getId());
        UserRsp userRsp = new UserRsp();
        BeanUtils.copyProperties(user2,userRsp);
        return userRsp;
    }





    /**
     * @description: 查询所有的用户
     * @param
     * @return
     */
    @Override
    public IPage<UserRsp> getPageVo(Page<?> page, UserQueryVo userQueryVo) {

        //分页查询
        IPage<UserRsp> pageVo = baseMapper.getPageVo(page, userQueryVo);
        return pageVo;
    }


    /**
     * @description: 商家审核成功注册账户
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void verify(String id, Integer status) {
        //TODO 商家如果审核通过，会往权限认证系统添加当前用户被自定义分配商家角色
            User user = baseMapper.selectById(id);
            CustomAssert.isBlank(user,ResultCodeEnum.USER_NOT_EXIST_ERROR.getMessage());
            user.setFreeze(status);
            baseMapper.updateById(user);
    }


    /**
     * @description: 删除用户
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteUser(String id) {
        User user = baseMapper.selectById(id);
        CustomAssert.isBlank(user,ResultCodeEnum.USER_NOT_EXIST_ERROR.getMessage());
        baseMapper.deleteById(user);
    }


    /**
     * @description: 根据微信登录id获取账户
     * @param
     * @return
     */
    @Override
    public UserRsp getByOpenid(String openid) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getOpenId,openid);
        User user = baseMapper.selectOne(wrapper);
        if (!ObjectUtils.isEmpty(user)){
            UserRsp userRsp = new UserRsp();
            BeanUtils.copyProperties(user,userRsp);
            return userRsp;
        }
        return null;
    }


    /**
     * @description: 根据当天日期查看注册用户人数
     * @param day 精确天
     * @return
     */
    @Override
    public Integer selectRegisterNumByDay(String day) {
        return baseMapper.selectRegisterNumByDay(day);
    }
}
